﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PRO.Entity;
using PRO.ORM;
using PRO.DbProvider;
using PRO.Common;
using PRO.Config;



namespace PRO.Admin
{
    public class AdminLib
    {
        /// <summary>
        /// 根据用户名获取管理员信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static AdminInfo GetAdminInfo(string username)
        {
            AdminInfo info = new AdminInfo();
            info.BindDataToEntity(DataBaseProvider.GetInstance().GetAdminInfo(username));
            return info;
        }
        /// <summary>
        /// 获取当前登陆用户信息
        /// </summary>
        /// <returns></returns>
        public static AdminInfo GetAdminInfo()
        {
            string username = Utils.GetCookie(Utils.MD5("username")).DEC_Decode(BaseConfigs.DEC_KEY);
            if (!username.IsNullOrWhiteSpace())
            {
                string pwd = Utils.GetCookie(Utils.MD5(username)).DEC_Decode(BaseConfigs.DEC_KEY);
                if (!pwd.IsNullOrWhiteSpace())
                {
                    AdminInfo info = AdminLib.GetAdminInfo(username);
                    if (info != null && info.ID > 0)
                    {
                        if (info.Pwd.ToString().Equals(pwd))
                        {
                            return info;
                        }
                    }
                }
            }
            return null;

        }
        /// <summary>
        /// 管理员登陆
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static bool Login(string username, string pwd, out string msg)
        {
            bool r = false;
            msg = "";
            AdminInfo info = AdminLib.GetAdminInfo(username);
            if (info != null && info.ID > 0)
            {
                if (info.Locked)
                    msg = "该用户已经被锁定，请联系管理员！";
                else
                {
                    if (info.Pwd.ToString().Equals(pwd))
                    {
                        r = true;
                        Utils.WriteCookie(Utils.MD5("username"), info.UserName.DEC_Encode(BaseConfigs.DEC_KEY), 60 * 2);
                        Utils.WriteCookie(Utils.MD5(info.UserName), pwd.DEC_Encode(BaseConfigs.DEC_KEY), 60 * 2);
                    }
                    else
                        msg = "密码不正确！";
                }
            }
            else
                msg = "用户名不存在！";
            return r;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="oldpwd"></param>
        /// <param name="newpwd"></param>
        /// <returns></returns>
        public static bool RePwd(int id, string oldpwd, string newpwd)
        {
            return DataBaseProvider.GetInstance().RePwd(id, oldpwd, newpwd);
        }
    }
}
